package com.dljd.javase.t12.threadpool;


import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class NoPool {
    public static void main(String[] args) {
        pool();
        noPool();
    }

    public static void pool(){
        long startTime=System.currentTimeMillis();
        CountDownLatch  d=new CountDownLatch(10000);
        final Vector  nums=new Vector();
        ExecutorService  pool= Executors.newSingleThreadExecutor();
//        Runnable r=new Runnable() {
//            @Override
//            public void run() {
//                   nums.add(10);
//                   d.countDown();
//            }
//        };
        Callable<Integer> r=new JisunCallable();
        for (int i = 0; i <10000 ; i++) {
            Future<Integer>  result=pool.submit(r);
            System.out.println(result);
        }


        try {
            d.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();

        System.out.println(endTime-startTime);
        pool.shutdown();
        System.out.println(nums.size());
    }

    public static void noPool(){
        long startTime=System.currentTimeMillis();
        CountDownLatch  d=new CountDownLatch(10000);
        final Vector  nums=new Vector();

        for (int i = 0; i <10000 ; i++) {
            new Thread(){
                @Override
                public void run() {
                    nums.add(10);
                    d.countDown();
                }
            }.start();
        }

        try {
            d.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();

        System.out.println(endTime-startTime);
        System.out.println(nums.size());
    }
}
